package com.jos.appium;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;

public class AndroidWeibo {

	public AndroidDriver<WebElement> getDriver() throws MalformedURLException {
		System.out.println("---getDriver start");
		DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
		desiredCapabilities.setCapability("platformName", "Android");
		desiredCapabilities.setCapability("deviceName", "127.0.0.1:7555");
		desiredCapabilities.setCapability("platformVersion", "6.0.1");
		desiredCapabilities.setCapability("appPackage", "com.sina.weibo");
		desiredCapabilities.setCapability("appActivity", "com.sina.weibo.SplashActivity");
		desiredCapabilities.setCapability("sessionOverride", true);
		desiredCapabilities.setCapability("noReset", true);
		desiredCapabilities.setCapability("fullReset", false);
		desiredCapabilities.setCapability("unicodeKeyboard", true);
		desiredCapabilities.setCapability("newCommandTimeout", "30000");
		desiredCapabilities.setCapability("showChromedriverLog", true);
		desiredCapabilities.setCapability("recreateChromeDriverSessions", "True");

		URL remoteUrl = new URL("http://localhost:4723/wd/hub");
		AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(remoteUrl, desiredCapabilities);

		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		System.out.println("---getDriver end");
		return driver;
	}

	public void openMessage() {
		AndroidDriver<WebElement> driver = null;
		try {
			driver = getDriver();

			execContent(driver, By.className("android.view.View"), 0, "标题");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		System.out.println("---openMessage end");
	}

	private void execContent(AndroidDriver<WebElement> driver, By by, int index, String comment)
			throws InterruptedException, IOException {
		Thread.sleep(30 * 1000);

		List<WebElement> elementList;
		int elementLength;
		WebElement element;
		elementList = driver.findElements(by);
		elementLength = elementList == null ? 0 : elementList.size();
		System.out.println(by + " find=" + elementLength);
		for (int i = 0; i < elementLength; i++) {
			element = elementList.get(i);

			byte[] bytes = element.getScreenshotAs(OutputType.BYTES);
			File file = new File("D:/file/s" + i + ".png");
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bytes);
			fos.flush();
			fos.close();
		}
		element = elementList.get(7);

		try {
			System.out.println(element.getAttribute("contentDescription"));
		} catch (Exception e) {
			System.out.println("contentDescription" + e);
		}

		try {
			System.out.println(element.getAttribute("text"));
		} catch (Exception e) {
			System.out.println("text" + e);
		}
		try {
			System.out.println(element.getAttribute("name"));
		} catch (Exception e) {
			System.out.println("name" + e);
		}

		Thread.sleep(2 * 1000);
	}

	public static void main(String[] args) {
		AndroidWeibo model = new AndroidWeibo();

		model.openMessage();
	}

}
